package com.xxxx.server.controller;

import com.xxxx.server.pojo.Position;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/basic")
public class PositionController {

    @Autowired
    private IPositionService iPositionService;
    /**
     * 获得职位相关信息列表
     */
    @ApiOperation(value = "职位信息的调取")
    @GetMapping("/pos")
    //用一个List装Position对象相关信息
    public List<Position> getPosInfo(){
        return iPositionService.list();
    }

    @ApiOperation(value = "职位信息的添加")
    @PostMapping("/pos")
    public RespBean addPosInfo(@RequestBody Position position){
        //添加创建时间
        position.setCreateDate(LocalDateTime.now());
        if(iPositionService.save(position)){
            //System.out.println("职位信息添加成功！");
            return RespBean.success("职位信息添加成功！");
        }
        return RespBean.error("职位信息添加失败！");
    }


    @ApiOperation(value = "职位信息的修改")
    @PutMapping("/pos")
    public RespBean updatePosInfo(@RequestBody Position position){
        if (iPositionService.updateById(position)){
            return RespBean.success("职位信息更新成功");
        }
        return RespBean.error("职位信息更新失败");
    }

    //删除：假删 修改enabled
    @ApiOperation(value = "职位信息的删除")
    @DeleteMapping("/pos/{id}")
    public RespBean delPosInfo(@PathVariable Integer id){
        if (iPositionService.removeById(id)){
            return RespBean.success("职位信息删除成功");
        }
        return RespBean.error("职位信息删除失败");
    }

    //多条删除:通过数组
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/pos")
    public RespBean delPosInfo(Integer[] ids){
        if (iPositionService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("职位信息删除成功");
        }
        return RespBean.error("职位信息删除失败");
    }


}
